﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartCollection
{
    class SmartCollectionLoad
    {
        TheGioiPhaiDep_DataEntities db = new TheGioiPhaiDep_DataEntities();
        SmartCollectionBase smartBase = new SmartCollectionBase(); 
     
        public List<Menu> LoadMenu(string url, string Cate, string subCate,string urlGoc)
        {

            List<Menu> listMenu = new List<Menu>();

            var web = new HtmlWeb();
            var doc = web.Load(url);
            //  doc.Load(new StringReader(s));

            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    var list = doc.DocumentNode.SelectNodes(Cate);

                    if (list != null)
                    {
                        //pro.Link = names.Attributes[0].Value;
                        // Console.WriteLine(names.Attributes[0].Value);
                        // duyet danh sach
                        for (int i = 0; i < list.Count(); i++)
                        {

                            //  var webPro = new HtmlWeb();
                            //  var proDoc=  webPro.Load(node.InnerHtml);
                            HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

                            proDoc.LoadHtml(list[i].InnerHtml);
                            //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");

                            var simpleMenu = proDoc.DocumentNode.SelectSingleNode("//a");
                            if (simpleMenu != null)
                            {
                                // lay ra link

                                Menu m = new Menu();
                                m.Id = simpleMenu.InnerText.convertToUnSign3().ToCutString();
                                m.Name = simpleMenu.InnerText;
                                for (int j = 0; j < simpleMenu.Attributes.Count(); j++)
                                {
                                    if (simpleMenu.Attributes[j].Name.Equals("href"))
                                    {
                                        m.Url = urlGoc + simpleMenu.Attributes[j].Value;
                                        break;
                                    }

                                }


                                //  WriteFile(names.Attributes[0].);

                                listMenu.Add(m);
                            }
                        }
                    }
                }
            }
           
            return listMenu;
        }
        public List<NumberPage> LoadPage(Menu menu, string Cate, string url,string urlGoc)
        {
            List<NumberPage> listPage = new List<NumberPage>();

            var web = new HtmlWeb();
            var doc = web.Load(menu.Url);
            //  doc.Load(new StringReader(s));

            {

                if (doc.DocumentNode != null)
                {


                    var list = doc.DocumentNode.SelectNodes(Cate);
                    if( list != null)
                    for (int i = 0; i < list.Count(); i++)
                    {
                        NumberPage np = new NumberPage();
                        np.Id = menu.Id;
                        np.Name = menu.Name;

                        np.Url = urlGoc + list[i].Attributes[0].Value;
                        listPage.Add(np);



                    }
                }
                return listPage;
            }
        }
        public List<NewsDetails> LoadHomeNews(NumberPage menu, string Cate, string subCate
            , string title, string shortDetail, string urlNews, string url, string urlGoc)
        {

            List<NewsDetails> listNewsDetails = new List<NewsDetails>();
           
            var web = new HtmlWeb();
             var doc = web.Load(menu.Url);
        //    var doc = web.Load("http://afamily.vn/dep.chn");

            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    var list = doc.DocumentNode.SelectNodes(Cate);

                    if (list != null)
                    {
                        //pro.Link = names.Attributes[0].Value;
                        // Console.WriteLine(names.Attributes[0].Value);
                        // duyet danh sach
                        for (int i = 0; i < list.Count(); i++)
                        {

                            //  var webPro = new HtmlWeb();
                            //  var proDoc=  webPro.Load(node.InnerHtml);
                            HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

                            proDoc.LoadHtml(list[i].InnerHtml);
                            //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");

                            var urlImg = proDoc.DocumentNode.SelectSingleNode(subCate + "//a//img");
                            NewsDetails nd = new NewsDetails();
                            if (urlImg != null )
                            {
                                for (int j = 0; j < urlImg.Attributes.Count(); j++)
                                {

                                    if (urlImg.Attributes[j].Name.Equals("src"))
                                    {
                                        nd.UrlImg = urlImg.Attributes[j].Value;

                                    }
                                }
                          
                                nd.CategoryNewsId = menu.Id;
                               
                                nd.ShortDescription = proDoc.DocumentNode.SelectSingleNode(shortDetail).InnerText;
                                nd.Title = proDoc.DocumentNode.SelectSingleNode(title).InnerText;
                                nd.Url = urlGoc + proDoc.DocumentNode.SelectSingleNode(urlNews).Attributes[0].Value;
                                listNewsDetails.Add(nd);
                            }
                         
                        }
                    }
                }
            }

            return listNewsDetails;
        }





        public NewsDetails LoadNews(NewsDetails news, string Cate)
        {
          List<NumberPage> listPage = new List<NumberPage>();

            var web = new HtmlWeb();
            var doc = web.Load(news.Url);
            //  doc.Load(new StringReader(s));


          
                if (doc.DocumentNode != null)
                {
                   
                
                   
                    //var test=  db.News.Where(m=>m.Url.Equals(news.Url)).ToList();



                    //smartBase.CheckUrlInDb(news.Url)== false &&

                    if ( smartBase.UrlVitedInToday(news.Url) == false && doc.DocumentNode.SelectSingleNode(Cate) != null)
                    {
                        News n = new News();
                        n.CreatedOnUtc = DateTime.Now;
                        n.HomePicture = news.UrlImg;
                        n.LanguageId = 2;
                        n.MetaDescription = news.Title;
                        n.MetaKeywords = news.Title;
                        n.MetaTitle = news.Title;
                        // n.ParentId = news.ParentId;
                        n.Short = news.ShortDescription;
                        n.Title = news.Title.ToCutString();
                        n.Published = true;
                        n.AllowComments = true;
                        n.CategoryNewsId = news.CategoryNewsId.ToCutString();
                        n.Full = doc.DocumentNode.SelectSingleNode(Cate).InnerHtml;

                        n.Url = news.Url;
                        var newNews = smartBase.SaveToNews(n);

                        smartBase.SaveToUrlRecord(newNews.Id, "NewsItem", n.Title.convertToUnSign3(), true, 2);
                        return news;
                      
                    }
                    return null;

                  
                }
                return null;
             

        }
        
     
        public UpdateNews LoadUpdateNewHomeNews(NumberPage menu, string Cate, string subCate
          , string title, string shortDetail, string urlNews, string url, string urlGoc,string date)
        {
            UpdateNews update= new UpdateNews();
            List<NewsDetails> listNewsDetails = new List<NewsDetails>();

            var web = new HtmlWeb();
            var doc = web.Load(menu.Url);
            //    var doc = web.Load("http://afamily.vn/dep.chn");

            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    var list = doc.DocumentNode.SelectNodes(Cate);

                    if (list != null)
                    {
                        //pro.Link = names.Attributes[0].Value;
                        // Console.WriteLine(names.Attributes[0].Value);
                        // duyet danh sach
                        for (int i = 0; i < list.Count(); i++)
                        {

                            //  var webPro = new HtmlWeb();
                            //  var proDoc=  webPro.Load(node.InnerHtml);
                            HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

                            proDoc.LoadHtml(list[i].InnerHtml);
                            //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");

                            var urlImg = proDoc.DocumentNode.SelectSingleNode(subCate + "//a//img");
                            NewsDetails nd = new NewsDetails();
                            var sdateNews = proDoc.DocumentNode.SelectSingleNode(date).InnerText;
                            DateTime dateNews= Convert.ToDateTime(sdateNews);

                            if (urlImg != null && dateNews.Date == DateTime.Today)
                            {
                                for (int j = 0; j < urlImg.Attributes.Count(); j++)
                                {

                                    if (urlImg.Attributes[j].Name.Equals("src"))
                                    {
                                        nd.UrlImg = urlImg.Attributes[j].Value;

                                    }
                                }

                                nd.CategoryNewsId = menu.Id;

                                nd.ShortDescription = proDoc.DocumentNode.SelectSingleNode(shortDetail).InnerText;
                                nd.Title = proDoc.DocumentNode.SelectSingleNode(title).InnerText;
                                nd.Url = urlGoc + proDoc.DocumentNode.SelectSingleNode(urlNews).Attributes[0].Value;
                                listNewsDetails.Add(nd);

                            }
                            else if (dateNews.Date < DateTime.Today)
                            {
                                update.flag = true;
                                break;
                            }

                        }
                    }
                }
            }
            update.ListNewsDetails = listNewsDetails;
            
            return update;
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
}
